package com.ruoyi.factorydata.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.ruoyi.common.utils.CacheSupportUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.factorydata.domain.*;
import com.ruoyi.factorydata.domain.dto.CraftRouteDTO;
import com.ruoyi.factorydata.domain.dto.ProcessDataDTO;
import com.ruoyi.factorydata.domain.vo.CraftProcessRelationVo;
import com.ruoyi.factorydata.domain.vo.CraftRouteVo;
import com.ruoyi.factorydata.domain.vo.ProcessDataVo;
import com.ruoyi.factorydata.mapper.*;
import com.ruoyi.factorydata.service.ICraftRouteService;
import com.ruoyi.factorydata.service.IProcessDataService;
import com.ruoyi.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 工艺路线Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-15
 */
@Slf4j
@Service
public class CraftRouteServiceImpl extends ServiceImpl<CraftRouteMapper, CraftRoute> implements ICraftRouteService {
    @Autowired
    private CraftRouteMapper craftRouteMapper;

    @Autowired
    private IProcessDataService processDataService;

    @Autowired
    private ProcessStationRelationMapper processStationRelationMapper;

    @Autowired
    private ProcessMaterialRelationMapper processMaterialRelationMapper;

    @Autowired
    private CraftProcessRelationMapper craftProcessRelationMapper;

    @Autowired
    private MaterialDataMapper materialDataMapper;

    @Autowired
    private MaterialCategoryMapper materialCategoryMapper;

    @Autowired
    private CraftProcessMaterialRelationMapper craftProcessMaterialRelationMapper;

    @Autowired
    private CraftProcessStationRelationMapper craftProcessStationRelationMapper;

    @Autowired
    private ProductionBomMapper productionBomMapper;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询工艺路线
     *
     * @param id 工艺路线主键
     * @return 工艺路线
     */
    @Override
    public CraftRouteVo selectCraftRouteById(String id) {
//        //查询工艺路线信息
        CraftRoute craftRoute = craftRouteMapper.selectCraftRouteById(id);
        //查询工艺路线相关工序信息
        List<CraftProcessRelation> craftProcessRelations = craftProcessRelationMapper.selectList(new QueryWrapper<CraftProcessRelation>().lambda()
                .eq(CraftProcessRelation::getCraftId, id));
        List<CraftProcessStationRelation> craftStationRelations = new ArrayList<>();
        List<CraftProcessMaterialRelation> craftProcessMaterialRelations = new ArrayList<>();
//        craftProcessRelations.forEach(craftProcessRelation -> {
//            //查询工艺路线相关工位信息
//            List<CraftProcessStationRelation> processStationRelations = craftProcessStationRelationMapper.selectList(new QueryWrapper<CraftProcessStationRelation>().lambda()
//                    .eq(CraftProcessStationRelation::getCraftId, id)
//                    .eq(CraftProcessStationRelation::getCraftProcessId, craftProcessRelation.getId()));
//            //查询工艺路线相关物料信息
//            List<CraftProcessMaterialRelation> processMaterialRelations = craftProcessMaterialRelationMapper.selectList(new QueryWrapper<CraftProcessMaterialRelation>().lambda()
//                    .eq(CraftProcessMaterialRelation::getCraftId, id)
//                    .eq(CraftProcessMaterialRelation::getCraftProcessId, craftProcessRelation.getId()));
//            craftStationRelations.addAll(processStationRelations);
//            craftProcessMaterialRelations.addAll(processMaterialRelations);
//        });
        HashMap<Long, String> processOwnerNameCache = new HashMap<>();

        List<CraftProcessRelationVo> craftProcessRelationVos = craftProcessRelations.stream().map(craftProcessRelation -> {
            //查询工艺路线相关工位信息
            List<CraftProcessStationRelation> processStationRelations = craftProcessStationRelationMapper.selectList(new QueryWrapper<CraftProcessStationRelation>().lambda()
                    .eq(CraftProcessStationRelation::getCraftId, id)
                    .eq(CraftProcessStationRelation::getStep, craftProcessRelation.getStep())
                    .eq(CraftProcessStationRelation::getCraftProcessId, craftProcessRelation.getId()));
            //查询工艺路线相关物料信息
            List<CraftProcessMaterialRelation> processMaterialRelations = craftProcessMaterialRelationMapper.selectList(new QueryWrapper<CraftProcessMaterialRelation>().lambda()
                    .eq(CraftProcessMaterialRelation::getCraftId, id)
                    .eq(CraftProcessMaterialRelation::getStep, craftProcessRelation.getStep())
                    .eq(CraftProcessMaterialRelation::getCraftProcessId, craftProcessRelation.getId()));
            List<ProcessDataDTO.Parameter> parameterList = null;
            if (craftProcessRelation.getTechnicalParameter() != null && !craftProcessRelation.getTechnicalParameter().isBlank()) {
                JSONArray jsonArray = JSON.parseArray(craftProcessRelation.getTechnicalParameter());
                parameterList = jsonArray.toList(ProcessDataDTO.Parameter.class);
            }
            String ownName = sysUserService.getUserNameByIdAndCache(craftProcessRelation.getProcessOrderOwnerId(), processOwnerNameCache);
            return CraftProcessRelationVo.builder()
                    .id(craftProcessRelation.getId())
                    .craftId(craftProcessRelation.getCraftId())
                    .step(craftProcessRelation.getStep())
                    .processOrderOwnerId(craftProcessRelation.getProcessOrderOwnerId())
                    .processOrderOwnerName(ownName)
                    .processId(craftProcessRelation.getProcessId())
                    .processCode(craftProcessRelation.getProcessCode())
                    .processName(craftProcessRelation.getProcessName())
                    .processTypeId(craftProcessRelation.getProcessTypeId())
                    .processTypeName(craftProcessRelation.getProcessTypeName())
                    .processCategoryId(craftProcessRelation.getProcessCategoryId())
                    .processCategoryName(craftProcessRelation.getProcessCategoryName())
                    .processImages(craftProcessRelation.getProcessImages())
                    .processAttachments(craftProcessRelation.getProcessAttachments())
                    .standardWorkingHours(craftProcessRelation.getStandardWorkingHours())
                    .auxiliaryWorkingHours(craftProcessRelation.getAuxiliaryWorkingHours())
                    .processCost(craftProcessRelation.getProcessCost())
                    .technicalParameter(craftProcessRelation.getTechnicalParameter())
                    .stationList(processStationRelations)
                    .materialList(processMaterialRelations)
                    .technicalParameterList(parameterList)
                    .delFlag(craftProcessRelation.getDelFlag())
                    .version(craftProcessRelation.getVersion())
                    .delFlag(craftProcessRelation.getDelFlag())
                    .remark(craftProcessRelation.getRemark())
                    .build();
        }).collect(Collectors.toList());

        return CraftRouteVo.builder()
                .id(craftRoute.getId())
                .productId(craftRoute.getProductId())
                .routeCode(craftRoute.getRouteCode())
                .routeName(craftRoute.getRouteName())
                .isExclusive(craftRoute.getIsExclusive())
                .status(craftRoute.getStatus())
                .craftAttachments(craftRoute.getCraftAttachments())
                .technicalParameterList(null)
                .craftProcessRelations(craftProcessRelationVos)
                .createBy(craftRoute.getCreateBy())
                .createTime(craftRoute.getCreateTime())
                .updateTime(craftRoute.getUpdateTime())
                .remark(craftRoute.getRemark())
                .build();
    }

    @Override
    public CraftRouteVo selectByCraftProcessRelation(CraftProcessRelation craftProcessRelation) {
//        //首先查下工艺工序表里有没有这个工序信息
//        CraftProcessRelation craftProcessRelationById = craftProcessRelationMapper.selectCraftProcessRelationById(craftProcessRelation.getId());
//        //没有则是新的，则查询模板表
//        if (craftProcessRelationById ==  null){
//
//            ArrayList<CraftProcessRelation> craftProcessRelations = new ArrayList<>();
//            ArrayList<CraftProcessStationRelation> craftProcessStationRelations = new ArrayList<>();
//            ArrayList<CraftProcessMaterialRelation>  craftMaterialRelations = new ArrayList<>();
//            // 查询工序模板表
//            ProcessDataVo processDataVo = processDataService.selectProcessDataById(craftProcessRelation.getProcessId());
//            // 查询工序工位模板表
//            List<ProcessStationRelation> processStationRelations = processStationRelationMapper.selectList(new QueryWrapper<ProcessStationRelation>().lambda()
//                    .eq(ProcessStationRelation::getProcessId, processDataVo.getId()));
//            processStationRelations.forEach(item -> {
//                CraftProcessStationRelation craftProcessStationRelation = new CraftProcessStationRelation();
//                craftProcessStationRelation.setProcessId(item.getProcessId());
//                craftProcessStationRelation.setStationId(item.getStationId());
//                craftProcessStationRelation.setStationCode(item.getStationCode());
//                craftProcessStationRelation.setStationName(item.getStationName());
//                craftProcessStationRelation.setWorkshopId(item.getWorkshopId());
//                craftProcessStationRelation.setWorkshopName(item.getWorkshopName());
//                craftProcessStationRelation.setProductionLineId(item.getProductionLineId());
//                craftProcessStationRelation.setProductionLineName(item.getProductionLineName());
//                craftProcessStationRelation.setFactoryId(item.getFactoryId());
//                craftProcessStationRelation.setFactoryName(item.getFactoryName());
//                craftProcessStationRelations.add(craftProcessStationRelation);
//
//            });
//            // 查询工序物料模板表
//            List<ProcessMaterialRelation> processMaterialRelations = processMaterialRelationMapper.selectList(new QueryWrapper<ProcessMaterialRelation>().lambda()
//                    .eq(ProcessMaterialRelation::getProcessId, processDataVo.getId()));
//            processMaterialRelations.forEach(item -> {
//                CraftProcessMaterialRelation craftProcessMaterialRelation = new CraftProcessMaterialRelation();
//                craftProcessMaterialRelation.setProcessId(item.getProcessId());
//                craftProcessMaterialRelation.setMaterialId(item.getMaterialId());
//                craftProcessMaterialRelation.setMaterialCode(item.getMaterialCode());
//                craftProcessMaterialRelation.setMaterialName(item.getMaterialName());
//                craftProcessMaterialRelation.setMaterialCategoryId(item.getMaterialCategoryId());
//                craftProcessMaterialRelation.setMaterialCategoryName(item.getMaterialCategoryName());
//                craftProcessMaterialRelation.setMaterialTypeId(item.getMaterialTypeId());
//                craftProcessMaterialRelation.setMaterialTypeName(item.getMaterialTypeName());
//                craftProcessMaterialRelation.setManufacturingMethodId(item.getManufacturingMethodId());
//                craftProcessMaterialRelation.setManufacturingMethodName(item.getManufacturingMethodName());
//                craftProcessMaterialRelation.setMaterialUnitId(item.getMaterialUnitId());
//                craftProcessMaterialRelation.setMaterialUnitName(item.getMaterialUnitName());
//                craftProcessMaterialRelation.setMaterialSpecification(item.getMaterialSpecification());
//                craftMaterialRelations.add(craftProcessMaterialRelation);
//            });
//            // 设置工序数据
//            CraftProcessRelation processRelation = new CraftProcessRelation();
//            processRelation.setProcessId(processDataVo.getId());
//            processRelation.setProcessCode(processDataVo.getProcessCode());
//            processRelation.setProcessName(processDataVo.getProcessName());
//            processRelation.setProcessTypeId(processDataVo.getProcessType());
//            processRelation.setProcessTypeName(processDataVo.getProcessTypeName());
//            processRelation.setProcessCategoryId(processDataVo.getCategoryId());
//            processRelation.setProcessCategoryName(processDataVo.getCategoryName());
//            processRelation.setProcessImages(processDataVo.getProcessImages());
//            processRelation.setProcessAttachments(processDataVo.getProcessAttachments());
//            processRelation.setTechnicalParameter(processDataVo.getTechnicalParameter());
//            processRelation.setStandardWorkingHours(processDataVo.getStandardWorkingHours());
//            processRelation.setAuxiliaryWorkingHours(processDataVo.getAuxiliaryWorkingHours());
//            processRelation.setProcessCost(processDataVo.getProcessCost());
//            processRelation.setTechnicalParameter(processDataVo.getTechnicalParameter());
//            processRelation.setTechnicalParameter(processDataVo.getTechnicalParameter());
//            craftProcessRelations.add(processRelation);
//            return CraftRouteVo.builder()
//                    .craftProcessRelations(craftProcessRelations)
//                    .craftProcessMaterialRelations(craftMaterialRelations)
//                    .craftProcessStationRelations(craftProcessStationRelations)
//                    .build();
//        }
//        // 查询工艺工序表
//        List<CraftProcessStationRelation> craftProcessStationRelations = craftProcessStationRelationMapper.selectList(new QueryWrapper<CraftProcessStationRelation>().lambda()
//                .eq(CraftProcessStationRelation::getCraftId, craftProcessRelation.getCraftId())
//                .eq(CraftProcessStationRelation::getProcessId, craftProcessRelation.getProcessId()));
//        List<CraftProcessMaterialRelation> craftProcessMaterialRelations = craftProcessMaterialRelationMapper.selectList(new QueryWrapper<CraftProcessMaterialRelation>().lambda()
//                .eq(CraftProcessMaterialRelation::getCraftId, craftProcessRelation.getCraftId())
//                .eq(CraftProcessMaterialRelation::getProcessId, craftProcessRelation.getProcessId()));
//        //获取工艺工序物料关系
//        ArrayList<CraftProcessRelation> craftProcessRelations = new ArrayList<>();
//        craftProcessRelations.add(craftProcessRelationById);
//        return CraftRouteVo.builder()
//                .craftProcessRelations(craftProcessRelations)
//                .build();
        return null;
    }

    @Override
    public List<CraftRouteVo> selectByIdsOrProcessIds(List<Long> ids, List<Long> processIds) {
        List<ProcessDataVo> processDataVos = processDataService.selectProcessDataByIds(processIds);
        List<CraftRouteVo> craftVos = processDataVos.stream().map(processData -> {
            //设置工艺工序数据

            //设置工艺工序工位数据
            List<ProcessStationRelation> processStationRelations = processStationRelationMapper.selectList(new QueryWrapper<ProcessStationRelation>().lambda()
                    .eq(ProcessStationRelation::getProcessId, processData.getId()));
            List<CraftProcessStationRelation> craftProcessStationRelations = processStationRelations.stream().map(processStationRelation -> {
                CraftProcessStationRelation craftProcessStationRelation = new CraftProcessStationRelation();
                craftProcessStationRelation.setProcessId(processStationRelation.getProcessId());
                craftProcessStationRelation.setStationId(processStationRelation.getStationId());
                craftProcessStationRelation.setStationCode(processStationRelation.getStationCode());
                craftProcessStationRelation.setStationName(processStationRelation.getStationName());
                craftProcessStationRelation.setWorkshopId(processStationRelation.getWorkshopId());
                craftProcessStationRelation.setWorkshopName(processStationRelation.getWorkshopName());
                craftProcessStationRelation.setProductionLineId(processStationRelation.getProductionLineId());
                craftProcessStationRelation.setProductionLineName(processStationRelation.getProductionLineName());
                craftProcessStationRelation.setFactoryId(processStationRelation.getFactoryId());
                craftProcessStationRelation.setFactoryName(processStationRelation.getFactoryName());
                craftProcessStationRelation.setEquipmentId(processStationRelation.getEquipmentId());
                craftProcessStationRelation.setEquipmentName(processStationRelation.getEquipmentName());
                craftProcessStationRelation.setCreateBy(processStationRelation.getCreateBy());
                craftProcessStationRelation.setCreateTime(DateUtils.getNowDate());
                return craftProcessStationRelation;
            }).collect(Collectors.toList());
            // 设置工艺工序物料表
            List<ProcessMaterialRelation> processMaterialRelations = processMaterialRelationMapper.selectList(new QueryWrapper<ProcessMaterialRelation>().lambda()
                    .eq(ProcessMaterialRelation::getProcessId, processData.getId()));
            List<CraftProcessMaterialRelation> materialRelations = processMaterialRelations.stream().map(processMaterialRelation -> {
                CraftProcessMaterialRelation craftProcessMaterialRelation = new CraftProcessMaterialRelation();
                craftProcessMaterialRelation.setId(processMaterialRelation.getId());
                craftProcessMaterialRelation.setProcessId(processMaterialRelation.getProcessId());
                craftProcessMaterialRelation.setMaterialId(processMaterialRelation.getMaterialId());
                craftProcessMaterialRelation.setMaterialName(processMaterialRelation.getMaterialName());
                craftProcessMaterialRelation.setMaterialCode(processMaterialRelation.getMaterialCode());
                craftProcessMaterialRelation.setMaterialCategoryId(processMaterialRelation.getMaterialCategoryId());
                craftProcessMaterialRelation.setMaterialCategoryName(processMaterialRelation.getMaterialCategoryName());
                craftProcessMaterialRelation.setMaterialTypeId(processMaterialRelation.getMaterialTypeId());
                craftProcessMaterialRelation.setMaterialTypeName(processMaterialRelation.getMaterialTypeName());
                craftProcessMaterialRelation.setManufacturingMethodId(processMaterialRelation.getManufacturingMethodId());
                craftProcessMaterialRelation.setManufacturingMethodName(processMaterialRelation.getManufacturingMethodName());
                craftProcessMaterialRelation.setMaterialUnitId(processMaterialRelation.getMaterialUnitId());
                craftProcessMaterialRelation.setMaterialUnitName(processMaterialRelation.getMaterialUnitName());
                craftProcessMaterialRelation.setMaterialSpecification(processMaterialRelation.getMaterialSpecification());
                craftProcessMaterialRelation.setMaterialUsedQuantity(processMaterialRelation.getMaterialUsedQuantity());
                return craftProcessMaterialRelation;
            }).collect(Collectors.toList());
            CraftProcessRelation craftProcessRelation = new CraftProcessRelation();
            craftProcessRelation.setProcessCost(processData.getProcessCost());
            List<ProcessDataDTO.Parameter> parameterList = null;
            if (processData.getTechnicalParameter() != null && !processData.getTechnicalParameter().isBlank()) {
                JSONArray jsonArray = JSON.parseArray(processData.getTechnicalParameter());
                parameterList = jsonArray.toList(ProcessDataDTO.Parameter.class);
            }
            CraftProcessRelationVo craftProcessRelationVo = CraftProcessRelationVo.builder()
                    .processId(processData.getId())
                    .processCode(processData.getProcessCode())
                    .processName(processData.getProcessName())
                    .processTypeId(processData.getProcessType())
                    .processTypeName(processData.getProcessTypeName())
                    .processCategoryId(processData.getCategoryId())
                    .processCategoryName(processData.getCategoryName())
                    .processImages(processData.getProcessImages())
                    .processAttachments(processData.getProcessAttachments())
                    .standardWorkingHours(processData.getStandardWorkingHours())
                    .auxiliaryWorkingHours(processData.getAuxiliaryWorkingHours())
                    .processCost(processData.getProcessCost())
                    .processAttachments(processData.getProcessAttachments())
                    .technicalParameterList(parameterList)
                    .stationList(craftProcessStationRelations)
                    .materialList(materialRelations)
                    .delFlag(processData.getDelFlag())
                    .version(processData.getVersion())
                    .createBy(processData.getCreateBy())
                    .createTime(processData.getCreateTime())
                    .updateBy(processData.getUpdateBy())
                    .updateTime(processData.getUpdateTime())
                    .remark(processData.getRemark())
                    .build();
            List<CraftProcessRelationVo> craftProcessRelationVos = new ArrayList<>();
            craftProcessRelationVos.add(craftProcessRelationVo);
            return CraftRouteVo.builder()
                    .craftProcessRelations(craftProcessRelationVos)
                    .build();
        }).collect(Collectors.toList());
        return craftVos;
    }


    /**
     * 查询工艺路线列表
     *
     * @param craftRoute 工艺路线
     * @return 工艺路线
     */
    @Override
    public List<CraftRouteVo> selectCraftRouteList(CraftRoute craftRoute) {
        List<CraftRoute> craftRoutes = craftRouteMapper.selectCraftRouteList(craftRoute);
        List<CraftRouteVo> craftVos = craftRoutes.stream().map(craft -> {
            ProductionBom productionBom = productionBomMapper.selectById(craft.getProductId());
            return CraftRouteVo.builder()
                    .id(craft.getId())
                    .productId(craft.getProductId())
                    .productName(productionBom != null ? productionBom.getMaterialName() : null)
                    .routeCode(craft.getRouteCode())
                    .routeName(craft.getRouteName())
                    .isExclusive(craft.getIsExclusive())
                    .status(craft.getStatus())
                    .craftTag(craft.getCraftTag())
                    .remark(craft.getRemark())
                    .createBy(craft.getCreateBy())
                    .updateTime(craft.getUpdateTime())
                    .build();
        }).collect(Collectors.toList());
        Page<CraftRoute> queryData=(Page<CraftRoute>)craftRoutes;
        Page<CraftRouteVo> page = new Page<>(queryData.getPageNum(), queryData.getPageSize());
        page.setTotal(queryData.getTotal());
        page.addAll(craftVos);
        return page;
    }

    @Override
    public List<CraftRouteVo> selectCraftRouteListByIds(List<Long> ids) {
        //查询工艺路线信息
        List<CraftRoute> craftRoutes = craftRouteMapper.selectList(new QueryWrapper<CraftRoute>().lambda()
                .in(CraftRoute::getId, ids));
        List<CraftRouteVo> craftRouteVos = new ArrayList<>();
        if (!craftRoutes.isEmpty()) {
            craftRouteVos = craftRoutes.stream().map(craftRoute -> {
                //查询工艺路线相关工序信息
                List<CraftProcessRelation> craftProcessRelations = craftProcessRelationMapper.selectList(new QueryWrapper<CraftProcessRelation>().lambda()
                        .eq(CraftProcessRelation::getCraftId, craftRoute.getId()));
                List<CraftProcessRelationVo> craftProcessRelationVos = craftProcessRelations.stream().map(craftProcessRelation -> {
                    //查询工艺路线相关工位信息
                    List<CraftProcessStationRelation> processStationRelations = craftProcessStationRelationMapper.selectList(new QueryWrapper<CraftProcessStationRelation>().lambda()
                            .eq(CraftProcessStationRelation::getCraftId, craftRoute.getId())
                            .eq(CraftProcessStationRelation::getStep, craftProcessRelation.getStep())
                            .eq(CraftProcessStationRelation::getCraftProcessId, craftProcessRelation.getId()));
                    //查询工艺路线相关物料信息
                    List<CraftProcessMaterialRelation> processMaterialRelations = craftProcessMaterialRelationMapper.selectList(new QueryWrapper<CraftProcessMaterialRelation>().lambda()
                            .eq(CraftProcessMaterialRelation::getCraftId, craftRoute.getId())
                            .eq(CraftProcessMaterialRelation::getStep, craftProcessRelation.getStep())
                            .eq(CraftProcessMaterialRelation::getCraftProcessId, craftProcessRelation.getId()));
                    List<ProcessDataDTO.Parameter> parameterList = null;
                    if (craftProcessRelation.getTechnicalParameter() != null && !craftProcessRelation.getTechnicalParameter().isBlank()) {
                        JSONArray jsonArray = JSON.parseArray(craftProcessRelation.getTechnicalParameter());
                        parameterList = jsonArray.toList(ProcessDataDTO.Parameter.class);
                    }
                    return CraftProcessRelationVo.builder()
                            .id(craftProcessRelation.getId())
                            .craftId(craftProcessRelation.getCraftId())
                            .step(craftProcessRelation.getStep())
                            .processId(craftProcessRelation.getProcessId())
                            .processCode(craftProcessRelation.getProcessCode())
                            .processName(craftProcessRelation.getProcessName())
                            .processTypeId(craftProcessRelation.getProcessTypeId())
                            .processTypeName(craftProcessRelation.getProcessTypeName())
                            .processCategoryId(craftProcessRelation.getProcessCategoryId())
                            .processCategoryName(craftProcessRelation.getProcessCategoryName())
                            .processImages(craftProcessRelation.getProcessImages())
                            .processAttachments(craftProcessRelation.getProcessAttachments())
                            .standardWorkingHours(craftProcessRelation.getStandardWorkingHours())
                            .auxiliaryWorkingHours(craftProcessRelation.getAuxiliaryWorkingHours())
                            .processCost(craftProcessRelation.getProcessCost())
                            .technicalParameter(craftProcessRelation.getTechnicalParameter())
                            .stationList(processStationRelations)
                            .materialList(processMaterialRelations)
                            .technicalParameterList(parameterList)
                            .delFlag(craftProcessRelation.getDelFlag())
                            .version(craftProcessRelation.getVersion())
                            .delFlag(craftProcessRelation.getDelFlag())
                            .remark(craftProcessRelation.getRemark())
                            .build();
                }).collect(Collectors.toList());
                MaterialData materialData = materialDataMapper.selectById(craftRoute.getProductId());
                MaterialCategory materialCategory = materialCategoryMapper.selectById(materialData.getCategoryId());
                return CraftRouteVo.builder()
                        .id(craftRoute.getId())
                        .productId(craftRoute.getProductId())
                        .productName(materialData.getMaterialName())
                        .productCategoryId(materialCategory.getId())
                        .productCategoryName(materialCategory.getMaterialCategoryName())
                        .routeCode(craftRoute.getRouteCode())
                        .routeName(craftRoute.getRouteName())
                        .isExclusive(craftRoute.getIsExclusive())
                        .status(craftRoute.getStatus())
                        .craftAttachments(craftRoute.getCraftAttachments())
                        .technicalParameterList(null)
                        .craftProcessRelations(craftProcessRelationVos)
                        .createBy(craftRoute.getCreateBy())
                        .createTime(craftRoute.getCreateTime())
                        .updateTime(craftRoute.getUpdateTime())
                        .remark(craftRoute.getRemark())
                        .build();
            }).collect(Collectors.toList());
        }

        return craftRouteVos;
    }

    /**
     * 新增工艺路线
     *
     * @param craftRouteDTO 工艺路线
     * @return 结果
     */
    @Transactional
    @Override
    public int insertCraftRoute(CraftRouteDTO craftRouteDTO) {
        CraftRoute craftRoute = new CraftRoute();
        craftRoute.setProductId(craftRouteDTO.getProductId());
        craftRoute.setRouteCode(craftRouteDTO.getRouteCode());
        craftRoute.setRouteName(craftRouteDTO.getRouteName());
        craftRoute.setStatus(craftRouteDTO.getStatus());
        craftRoute.setCraftAttachments(craftRouteDTO.getCraftAttachments());
        craftRoute.setIsExclusive(craftRouteDTO.getIsExclusive());
        craftRoute.setRemark(craftRouteDTO.getRemark());
        craftRoute.setCreateTime(DateUtils.getNowDate());
        int i = craftRouteMapper.insertCraftRoute(craftRoute);
        if (i > 0) {
            //添加工艺工序关联
            log.info("craftRoute:{}", craftRoute);
            log.info("添加工艺工序关联:{}", craftRouteDTO.getCraftProcessRelations());
            craftRouteDTO.getCraftProcessRelations().forEach(craftProcessRelation -> {
                CraftProcessRelation processRelation = new CraftProcessRelation();
                processRelation.setCraftId(craftRoute.getId());
                processRelation.setStep(craftProcessRelation.getStep());
                processRelation.setProcessOrderOwnerId(craftProcessRelation.getProcessOrderOwnerId());
                processRelation.setProcessId(craftProcessRelation.getProcessId());
                processRelation.setProcessCode(craftProcessRelation.getProcessCode());
                processRelation.setProcessName(craftProcessRelation.getProcessName());
                processRelation.setProcessTypeId(craftProcessRelation.getProcessTypeId());
                processRelation.setProcessTypeName(craftProcessRelation.getProcessTypeName());
                processRelation.setProcessCategoryId(craftProcessRelation.getProcessCategoryId());
                processRelation.setProcessCategoryName(craftProcessRelation.getProcessCategoryName());
                processRelation.setProcessImages(craftProcessRelation.getProcessImages());
                processRelation.setProcessAttachments(craftProcessRelation.getProcessAttachments());
                processRelation.setStandardWorkingHours(craftProcessRelation.getStandardWorkingHours());
                processRelation.setAuxiliaryWorkingHours(craftProcessRelation.getAuxiliaryWorkingHours());
                processRelation.setRemark(craftProcessRelation.getRemark());
                processRelation.setProcessCost(craftProcessRelation.getProcessCost());
                //格式化
                if (craftProcessRelation.getTechnicalParameterList() != null && !craftProcessRelation.getTechnicalParameterList().isEmpty()) {
                    processRelation.setTechnicalParameter(JSON.toJSONString(craftProcessRelation.getTechnicalParameterList()));
                } else {
                    processRelation.setTechnicalParameter(null);
                }
                processRelation.setRemark(craftProcessRelation.getRemark());
                processRelation.setCreateBy(craftRouteDTO.getCreateBy());
                processRelation.setCreateTime(craftProcessRelation.getCreateTime());

                int i1 = craftProcessRelationMapper.insertCraftProcessRelation(processRelation);
                if (i1 > 0) {
                    // 添加工艺工序工位关联
                    craftProcessRelation.getStationList().forEach(craftProcessStationRelation -> {
                        CraftProcessStationRelation processStationRelation = new CraftProcessStationRelation();
                        processStationRelation.setCraftId(craftRoute.getId());
                        processStationRelation.setCraftProcessId(processRelation.getId());
                        processStationRelation.setStep(processRelation.getStep());
                        processStationRelation.setProcessId(craftProcessStationRelation.getProcessId());
                        processStationRelation.setStationId(craftProcessStationRelation.getStationId());
                        processStationRelation.setStationCode(craftProcessStationRelation.getStationCode());
                        processStationRelation.setStationName(craftProcessStationRelation.getStationName());
                        processStationRelation.setWorkshopId(craftProcessStationRelation.getWorkshopId());
                        processStationRelation.setWorkshopName(craftProcessStationRelation.getWorkshopName());
                        processStationRelation.setProductionLineId(craftProcessStationRelation.getProductionLineId());
                        processStationRelation.setProductionLineName(craftProcessStationRelation.getProductionLineName());
                        processStationRelation.setFactoryId(craftProcessStationRelation.getFactoryId());
                        processStationRelation.setFactoryName(craftProcessStationRelation.getFactoryName());
                        processStationRelation.setEquipmentId(craftProcessStationRelation.getEquipmentId());
                        processStationRelation.setEquipmentName((craftProcessStationRelation.getEquipmentName()));
                        processStationRelation.setCreateTime(DateUtils.getNowDate());
                        processStationRelation.setCreateBy(craftRouteDTO.getCreateBy());
                        log.info("新增工序工位关系数据：{}", processStationRelation);
                        craftProcessStationRelationMapper.insertCraftProcessStationRelation(processStationRelation);
                    });

                    // 添加工艺工序物料关联
                    log.info("将数据新增到工序物料关联表中:{}", craftProcessRelation.getMaterialList());
                    craftProcessRelation.getMaterialList().forEach(craftProcessMaterialRelation -> {
                        CraftProcessMaterialRelation processMaterialRelation = new CraftProcessMaterialRelation();
                        processMaterialRelation.setCraftId(craftRoute.getId());
                        processMaterialRelation.setCraftProcessId(processRelation.getId());
                        processMaterialRelation.setStep(processRelation.getStep());
                        processMaterialRelation.setProcessId(craftProcessMaterialRelation.getProcessId());
                        processMaterialRelation.setMaterialId(craftProcessMaterialRelation.getMaterialId());
                        processMaterialRelation.setMaterialName(craftProcessMaterialRelation.getMaterialName());
                        processMaterialRelation.setMaterialCode(craftProcessMaterialRelation.getMaterialCode());
                        processMaterialRelation.setMaterialCategoryId(craftProcessMaterialRelation.getMaterialCategoryId());
                        processMaterialRelation.setMaterialCategoryName(craftProcessMaterialRelation.getMaterialCategoryName());
                        processMaterialRelation.setMaterialTypeId(craftProcessMaterialRelation.getMaterialTypeId());
                        processMaterialRelation.setMaterialTypeName(craftProcessMaterialRelation.getMaterialTypeName());
                        processMaterialRelation.setManufacturingMethodId(craftProcessMaterialRelation.getManufacturingMethodId());
                        processMaterialRelation.setManufacturingMethodName(craftProcessMaterialRelation.getManufacturingMethodName());
                        processMaterialRelation.setMaterialUnitId(craftProcessMaterialRelation.getMaterialUnitId());
                        processMaterialRelation.setMaterialUnitName(craftProcessMaterialRelation.getMaterialUnitName());
                        processMaterialRelation.setMaterialSpecification(craftProcessMaterialRelation.getMaterialSpecification());
                        processMaterialRelation.setMaterialUsedQuantity(craftProcessMaterialRelation.getMaterialUsedQuantity());
                        processMaterialRelation.setRemark(craftProcessMaterialRelation.getRemark());
                        processMaterialRelation.setCreateTime(DateUtils.getNowDate());
                        processMaterialRelation.setCreateBy(craftRouteDTO.getCreateBy());
                        craftProcessMaterialRelationMapper.insertCraftProcessMaterialRelation(processMaterialRelation);
                    });
                }

            });

        }
        return i;
    }

    /**
     * 修改工艺路线
     *
     * @param craftRouteDTO 工艺路线
     * @return 结果
     */
    @Transactional
    @Override
    public int updateCraftRoute(CraftRouteDTO craftRouteDTO) {
        CraftRoute craftRoute = new CraftRoute();
        craftRoute.setId(craftRouteDTO.getId());
        craftRoute.setProductId(craftRouteDTO.getProductId());
        craftRoute.setRouteCode(craftRouteDTO.getRouteCode());
        craftRoute.setRouteName(craftRouteDTO.getRouteName());
        craftRoute.setIsExclusive(craftRouteDTO.getIsExclusive());
        craftRoute.setStatus(craftRouteDTO.getStatus());
        craftRoute.setCraftAttachments(craftRouteDTO.getCraftAttachments());
        craftRoute.setIsExclusive(craftRouteDTO.getIsExclusive());
        craftRoute.setRemark(craftRouteDTO.getRemark());
        craftRoute.setUpdateTime(DateUtils.getNowDate());
        int i = craftRouteMapper.updateCraftRoute(craftRoute);
        if (i > 0) {
            // 删除以前关联的数据
            craftProcessRelationMapper.delete(new QueryWrapper<CraftProcessRelation>().lambda()
                    .eq(CraftProcessRelation::getCraftId, craftRoute.getId()));
            craftProcessStationRelationMapper.delete(new QueryWrapper<CraftProcessStationRelation>().lambda()
                    .eq(CraftProcessStationRelation::getCraftId, craftRoute.getId()));
            craftProcessMaterialRelationMapper.delete(new QueryWrapper<CraftProcessMaterialRelation>().lambda()
                    .eq(CraftProcessMaterialRelation::getCraftId, craftRoute.getId()));
            //添加工艺工序关联
            log.info("craftRoute:{}", craftRoute);
            log.info("添加工艺工序关联:{}", craftRouteDTO.getCraftProcessRelations());
            craftRouteDTO.getCraftProcessRelations().forEach(craftProcessRelation -> {
                CraftProcessRelation processRelation = new CraftProcessRelation();
                processRelation.setCraftId(craftRoute.getId());
                processRelation.setStep(craftProcessRelation.getStep());
                processRelation.setProcessOrderOwnerId(craftProcessRelation.getProcessOrderOwnerId());
                processRelation.setProcessId(craftProcessRelation.getProcessId());
                processRelation.setProcessCode(craftProcessRelation.getProcessCode());
                processRelation.setProcessName(craftProcessRelation.getProcessName());
                processRelation.setProcessTypeId(craftProcessRelation.getProcessTypeId());
                processRelation.setProcessTypeName(craftProcessRelation.getProcessTypeName());
                processRelation.setProcessCategoryId(craftProcessRelation.getProcessCategoryId());
                processRelation.setProcessCategoryName(craftProcessRelation.getProcessCategoryName());
                processRelation.setProcessImages(craftProcessRelation.getProcessImages());
                processRelation.setProcessAttachments(craftProcessRelation.getProcessAttachments());
                processRelation.setStandardWorkingHours(craftProcessRelation.getStandardWorkingHours());
                processRelation.setAuxiliaryWorkingHours(craftProcessRelation.getAuxiliaryWorkingHours());
                processRelation.setProcessCost(craftProcessRelation.getProcessCost());
                //格式化
                if (craftProcessRelation.getTechnicalParameterList() != null && !craftProcessRelation.getTechnicalParameterList().isEmpty()) {
                    processRelation.setTechnicalParameter(JSON.toJSONString(craftProcessRelation.getTechnicalParameterList()));
                } else {
                    processRelation.setTechnicalParameter(null);
                }
                processRelation.setRemark(craftProcessRelation.getRemark());
                processRelation.setCreateBy(craftRouteDTO.getCreateBy());
                processRelation.setCreateTime(craftProcessRelation.getCreateTime());

                int i1 = craftProcessRelationMapper.insertCraftProcessRelation(processRelation);
                if (i1 > 0) {
                    // 添加工艺工序工位关联
                    craftProcessRelation.getStationList().forEach(craftProcessStationRelation -> {
                        CraftProcessStationRelation processStationRelation = new CraftProcessStationRelation();
                        processStationRelation.setCraftId(craftRoute.getId());
                        processStationRelation.setCraftProcessId(processRelation.getId());
                        processStationRelation.setStep(processRelation.getStep());
                        processStationRelation.setProcessId(craftProcessStationRelation.getProcessId());
                        processStationRelation.setStationId(craftProcessStationRelation.getStationId());
                        processStationRelation.setStationCode(craftProcessStationRelation.getStationCode());
                        processStationRelation.setStationName(craftProcessStationRelation.getStationName());
                        processStationRelation.setWorkshopId(craftProcessStationRelation.getWorkshopId());
                        processStationRelation.setWorkshopName(craftProcessStationRelation.getWorkshopName());
                        processStationRelation.setProductionLineId(craftProcessStationRelation.getProductionLineId());
                        processStationRelation.setProductionLineName(craftProcessStationRelation.getProductionLineName());
                        processStationRelation.setFactoryId(craftProcessStationRelation.getFactoryId());
                        processStationRelation.setFactoryName(craftProcessStationRelation.getFactoryName());
                        processStationRelation.setEquipmentId(craftProcessStationRelation.getEquipmentId());
                        processStationRelation.setEquipmentName(craftProcessStationRelation.getEquipmentName());
                        processStationRelation.setRemark(craftProcessStationRelation.getRemark());
                        processStationRelation.setCreateTime(DateUtils.getNowDate());
                        processStationRelation.setCreateBy(craftRouteDTO.getCreateBy());
                        log.info("新增工序工位关系数据：{}", processStationRelation);
                        craftProcessStationRelationMapper.insertCraftProcessStationRelation(processStationRelation);
                    });

                    // 添加工艺工序物料关联
                    log.info("将数据新增到工序物料关联表中:{}", craftProcessRelation.getMaterialList());
                    craftProcessRelation.getMaterialList().forEach(craftProcessMaterialRelation -> {
                        CraftProcessMaterialRelation processMaterialRelation = new CraftProcessMaterialRelation();
                        processMaterialRelation.setCraftId(craftRoute.getId());
                        processMaterialRelation.setCraftProcessId(processRelation.getId());
                        processMaterialRelation.setStep(processRelation.getStep());
                        processMaterialRelation.setProcessId(craftProcessMaterialRelation.getProcessId());
                        processMaterialRelation.setMaterialId(craftProcessMaterialRelation.getMaterialId());
                        processMaterialRelation.setMaterialName(craftProcessMaterialRelation.getMaterialName());
                        processMaterialRelation.setMaterialCode(craftProcessMaterialRelation.getMaterialCode());
                        processMaterialRelation.setMaterialCategoryId(craftProcessMaterialRelation.getMaterialCategoryId());
                        processMaterialRelation.setMaterialCategoryName(craftProcessMaterialRelation.getMaterialCategoryName());
                        processMaterialRelation.setMaterialTypeId(craftProcessMaterialRelation.getMaterialTypeId());
                        processMaterialRelation.setMaterialTypeName(craftProcessMaterialRelation.getMaterialTypeName());
                        processMaterialRelation.setManufacturingMethodId(craftProcessMaterialRelation.getManufacturingMethodId());
                        processMaterialRelation.setManufacturingMethodName(craftProcessMaterialRelation.getManufacturingMethodName());
                        processMaterialRelation.setMaterialUnitId(craftProcessMaterialRelation.getMaterialUnitId());
                        processMaterialRelation.setMaterialUnitName(craftProcessMaterialRelation.getMaterialUnitName());
                        processMaterialRelation.setMaterialSpecification(craftProcessMaterialRelation.getMaterialSpecification());
                        processMaterialRelation.setMaterialUsedQuantity(craftProcessMaterialRelation.getMaterialUsedQuantity());
                        processMaterialRelation.setRemark(craftProcessMaterialRelation.getRemark());
                        processMaterialRelation.setCreateTime(DateUtils.getNowDate());
                        processMaterialRelation.setCreateBy(craftRouteDTO.getCreateBy());
                        craftProcessMaterialRelationMapper.insertCraftProcessMaterialRelation(processMaterialRelation);
                    });
                }

            });
        }

        return i;
    }

    /**
     * 批量删除工艺路线
     *
     * @param ids 需要删除的工艺路线主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCraftRouteByIds(String[] ids) {
        int i = craftRouteMapper.deleteBatchIds(Arrays.stream(ids).collect(Collectors.toList()));
        if (i > 0) {
            // 删除以前关联的数据
            craftProcessRelationMapper.delete(new QueryWrapper<CraftProcessRelation>().lambda()
                    .in(CraftProcessRelation::getCraftId, ids));
            craftProcessStationRelationMapper.delete(new QueryWrapper<CraftProcessStationRelation>().lambda()
                    .in(CraftProcessStationRelation::getCraftId, ids));
            craftProcessMaterialRelationMapper.delete(new QueryWrapper<CraftProcessMaterialRelation>().lambda()
                    .in(CraftProcessMaterialRelation::getCraftId, ids));
        }
        return i;
    }

    /**
     * 删除工艺路线信息
     *
     * @param id 工艺路线主键
     * @return 结果
     */
    @Override
    public int deleteCraftRouteById(String id) {
        return craftRouteMapper.deleteCraftRouteById(id);
    }

    @Override
    public String getNameByIdAndCache(Long craftRouteId, Map<Long, String> craftRouteNameMap) {
        return CacheSupportUtils.getFromCacheOrDB(craftRouteId, craftRouteNameMap, this::getById, CraftRoute::getRouteName, false);
    }
}